package com.pentajock.docdb.types;

import com.pentajock.docdb.database.DBController;
import com.pentajock.docdb.utils.Utils;
import java.util.Date;
import java.io.File;

public class WeightedDocument extends Document {
	private int weight;
	/*
	 * private constructor called from public one below.
	 * WeightedDocuments should always be created from an
	 * existing Document reference so that it can grab
	 * it's information including its properties.
	 */
	private WeightedDocument(int documentID,
							String departmentName,
							String documentName,
							String extension,
							String category,
							Date effectiveDate,
							int updateInterval,
							String path,
							boolean isApproved,
							String newDocumentName,
							String assignee,
							String activeXCall,
							int weight) {
		super(documentID, departmentName, documentName, extension, category, effectiveDate, updateInterval, path, isApproved, newDocumentName, assignee, activeXCall);
	}
	
	public WeightedDocument(Document doc, int weight) {
		this(doc.getDocumentID(),
			 doc.getDepartmentName(),
			 doc.getDocumentName(),
			 doc.getExtension(),
			 doc.getCategory(),
			 doc.getEffectiveDate(),
			 doc.getUpdateInterval(),
			 doc.getPath(),
			 doc.isApproved(),
			 doc.getNewDocumentName(),
			 doc.getAssignee(),
			 doc.getActiveXCall(),
			 weight
		);
		this.weight = weight;
		setProperties(doc.getProperties());
	}
	
	/*
	 * Thrown together for File support, needs work
	 */
	public WeightedDocument(File f, int weight) {
		this(-1, 
			"None",
			Utils.getSubject(f), 
			Utils.getExtension(f), 
			"Unknown",
			new Date(),
			-1,
			f.getParent(),
			true,
			null,
			null,
			DBController.getActiveXCall(f),
			weight
		);
		this.weight = weight;
	}
	
	public int getWeight() {
		return weight;
	}
	
	public boolean weightEquals(int weight) {
		return this.weight == weight;
	}
	
	public boolean equals(Object o) {
		if(o != null && o instanceof WeightedDocument) {
			WeightedDocument other = (WeightedDocument)o;
			return getWeight() == other.getWeight() && getDocumentName().equals(other.getDocumentName());
		}
		return false;
	}
	
	public int compareTo(Document doc) {
		// check weights.
		if(doc instanceof WeightedDocument) {
			WeightedDocument wdoc = (WeightedDocument)doc;
			if(getWeight() < wdoc.getWeight()) {
				return 1;     
			}
			if(getWeight() > wdoc.getWeight()) {
				return -1;
			}
		}
		// same weight, use documentName's compareTo;
		return getDocumentName().compareTo(doc.getDocumentName());
	}
}
